package com.ewuyong.concurrence.example.sync;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class SynchronizedTest {

    public void test1(int j) {
        synchronized (this) {
            for (int i = 0; i < 10; i++){
                log.info("test1 :{} --- {}", j, i);
            }

        }
    }

    public synchronized void test2(int j) {
            for (int i = 0; i < 10; i++){
                log.info("test1 :{} ---- {}", j, i);
            }
    }

    public void test3(int j) {
        synchronized (SynchronizedTest.class) {
            for (int i = 0; i < 10; i++){
                log.info("test1 :{} --- {}", j, i);
            }

        }
    }

    public static synchronized void test4(int j) {
        for (int i = 0; i < 10; i++){
            log.info("test1 :{} ---- {}", j, i);
        }
    }

    public static void main(String[] args) {
        SynchronizedTest synchronizedTest1 = new SynchronizedTest();
        SynchronizedTest synchronizedTest2 = new SynchronizedTest();
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        executorService1.execute(() -> {
            synchronizedTest1.test1(1);
        });
        ExecutorService executorService2 = Executors.newCachedThreadPool();
        executorService1.execute(() -> {
            synchronizedTest2.test1(2);
        });

    }
}
